package cn.edu.hbpu.controller.v1;

import cn.edu.hbpu.common.R;
import cn.edu.hbpu.dto.UserDto;
import cn.edu.hbpu.entity.sys.SysUser;
import cn.edu.hbpu.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.Map;


/**
 * @author lcy
 * @date 2022/8/4 11:56
 * @description 系统用户控制器
 */

@Slf4j
@RestController
@RequestMapping("/adminUser")
public class SysUserController {
    @Autowired
    private SysUserService sysUserService;

    /**
     * 生成验证码
     *
     * @return
     */
    @GetMapping("/code")
    public R<Map<Object, Object>> code() throws IOException {
        return sysUserService.generateCode();
    }

    /**
     * 用户登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<Map<Object, Object>> login(@RequestBody UserDto user) {
        return sysUserService.login(user);
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public R<Map<Object, Object>> logout() {
        return sysUserService.logout();
    }

    /**
     * 获取用户个人信息
     * @return
     */
    @GetMapping("/profile")
    public R<SysUser> getUserInfo() {
        return sysUserService.getUserInfo();
    }

    /**
     * 修改系统用户密码
     *
     * @param userId 用户id
     * @return
     */
    @PutMapping("/updatePwd/{userId}")
    public R<String> resetPwd(@PathVariable("userId") String userId, String newPwd) {
        return sysUserService.updatePwd(userId, newPwd);
    }

    /**
     * 修改账号状态
     *
     * @param status
     * @return
     */
    @PutMapping("/{userId}/{status}")
    public R<String> changeStatus(@PathVariable("userId") String userId, @PathVariable("status") int status) {
        return sysUserService.changeStatus(userId, status);
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/{userId}")
    public R<String> deleteUser(@PathVariable("userId") String userId) {
        return sysUserService.deleteUser(userId);
    }

    /**
     * 分页查询了管理员列表。实现了搜索功能
     * @param searchField
     * @param searchText
     * @param startDateTime
     * @param endDateTime
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/pagingQuery")
    public R<Page<SysUser>> pagingQuery(String searchField, String searchText, String startDateTime, String endDateTime, @NotNull int page, @NotNull int pageSize) {
        return sysUserService.pagingQuery(searchField, searchText, startDateTime, endDateTime, page, pageSize);
    }
}
